home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  12.6 KB  |  566 lines

  1. /* commands.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: commands.c,v 4.9 1995/02/14 04:02:28 espie Exp $
  6.  * $Log: commands.c,v $
  7.  * Revision 4.9  1995/02/14  04:02:28  espie
  8.  * Nothing
  9.  *
  10.  * Revision 4.9  1995/02/14  04:02:28  espie
  11.  * Nothing
  12.  *
  13.  * Revision 4.8  1995/02/06  14:50:47  espie
  14.  * Changed sample_info.
  15.  *
  16.  * Revision 4.8  1995/02/06  14:50:47  espie
  17.  * Changed sample_info.
  18.  *
  19.  * Revision 4.7  1995/02/01  20:41:45  espie
  20.  * *** empty log message ***
  21.  *
  22.  * Revision 4.6  1995/02/01  16:39:04  espie
  23.  * Includes moved to defs.h
  24.  *
  25.  * Revision 4.6  1995/02/01  16:39:04  espie
  26.  * Includes moved to defs.h
  27.  *
  28.  * Revision 4.2  1994/08/23  18:19:46  espie
  29.  * Added speedmode option
  30.  * Abstracted IO.
  31.  * Some notice to status.
  32.  * play_note instead of ch->mode.
  33.  * Fixed bug with bad loops.
  34.  * Modified the way set_speed works.
  35.  * Very small bug with volume (Lawrence).
  36.  * Added bg/fg test.
  37.  * More precise vibrato table.
  38.  *
  39.  * Revision 2.12  1992/11/13  13:24:24  espie
  40.  * Added some extended commands: E12AB, and some.
  41.  * now use set_volume in audio.c. All the device-dependent operation
  42.  * is there.
  43.  * Defensive programming: check the range of each note
  44.  * for arpeggio setup.
  45.  * Structured part of the code, especially replay ``automaton''
  46.  * and setting up of effects.
  47.  *
  48.  * Revision 1.9  1991/11/17  17:09:53  espie
  49.  * Added missing prototypes.
  50.  * Dynamic oversample and frequency.
  51.  * Added arpeggio.
  52.  * Fixed up vibrato depth.
  53.  * Added vibslide and portaslide.
  54.  * Added command 9.
  55.  */
  56.  
  57.  
  58. #include "defs.h"
  59. #include "channel.h"
  60. #include "song.h"
  61. #include "extern.h"
  62. #include "prefs.h"
  63.      
  64. ID("$Id: commands.c,v 4.9 1995/02/14 04:02:28 espie Exp $")
  65.  
  66. /* sine table for the vibrato effect (obtained through build.c) */
  67.  
  68. int vibrato_table[64] = 
  69.    {
  70.    0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  71.    510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  72.    -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  73.    -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  74.    -195,-148,-99,-49
  75.    };
  76.  
  77. /***
  78.  *
  79.  *
  80.  * setting up effects/doing effects.
  81.  * The set_xxx gets called while parsing the effect,
  82.  * the do_xxx gets called each tick, and update the
  83.  * sound parameters while playing it.
  84.  *
  85.  *
  86.  ***/
  87.  
  88.  
  89. void do_nothing(ch)
  90. struct channel *ch;
  91.    {
  92.    }
  93.  
  94. LOCAL void set_nothing(a, ch)
  95. struct automaton *a;
  96. struct channel *ch;
  97.    {
  98.    }
  99.  
  100. /* slide pitch (up or down) */
  101. LOCAL void do_slide(ch)
  102. struct channel *ch;
  103.    {
  104.    ch->pitch += ch->slide;
  105.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  106.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  107.    set_current_pitch(ch, ch->pitch);
  108.    }
  109.  
  110. LOCAL void set_upslide(a, ch)
  111. struct automaton *a;
  112. struct channel *ch;
  113.    {
  114.    ch->adjust = do_slide;
  115.    if (a->para)
  116.       ch->slide = a->para;
  117.    }
  118.  
  119. LOCAL void set_downslide(a, ch)
  120. struct automaton *a;
  121. struct channel *ch;
  122.    {
  123.    ch->adjust = do_slide;
  124.    if (a->para)
  125.       ch->slide = -a->para;
  126.    }
  127.  
  128. /* modulating the pitch with vibrato */
  129. LOCAL void do_vibrato(ch)
  130. struct channel *ch;
  131.    {
  132.    int offset;
  133.  
  134.       /* this is no longer a literal transcription of the pt
  135.        * code. I have rescaled the vibrato table.
  136.        */
  137.    ch->viboffset += ch->vibrate;
  138.    ch->viboffset &= 63;
  139.       /* please don't use logical shift on signed values */
  140.    offset = (vibrato_table[ch->viboffset] * ch->vibdepth)/256;
  141.       /* temporary update of only the step value,
  142.        * note that we do not change the saved pitch.
  143.        */
  144.    set_current_pitch(ch, ch->pitch + offset);
  145.    }
  146.  
  147. LOCAL void set_vibrato(a, ch)
  148. struct automaton *a;
  149. struct channel *ch;
  150.    {
  151.    ch->adjust = do_vibrato;
  152.    if (HI(a->para))
  153.       ch->vibrate = HI(a->para);
  154.    if (LOW(a->para))
  155.       ch->vibdepth = LOW(a->para);
  156.    }
  157.  
  158. /* arpeggio looks a bit like chords: we alternate between two
  159.  * or three notes very fast.
  160.  */
  161. LOCAL void do_arpeggio(ch)
  162. struct channel *ch;
  163.    {
  164.    if (++ch->arpindex >= MAX_ARP)
  165.       ch->arpindex =0;
  166.    set_current_pitch(ch, ch->arp[ch->arpindex]);
  167.    }
  168.  
  169. LOCAL void set_arpeggio(a, ch)
  170. struct automaton *a;
  171. struct channel *ch;
  172.    {
  173.       /* arpeggio can be installed relative to the
  174.        * previous note, so we have to check that there
  175.        * actually is a current(previous) note
  176.        */
  177.    if (ch->note == NO_NOTE)
  178.       {
  179.       status("No note present for arpeggio");
  180.       error = FAULT;
  181.       }
  182.    else
  183.       {
  184.       int note;
  185.  
  186.       ch->arp[0] = pitch_table[ch->note][ch->finetune];
  187.       note = ch->note + HI(a->para);
  188.       if (note < NUMBER_NOTES)
  189.          ch->arp[1] = pitch_table[note][ch->finetune];
  190.       else
  191.          {
  192.          status("Arpeggio note out of range");
  193.          error = FAULT;
  194.          }
  195.       note = ch->note + LOW(a->para);
  196.       if (note < NUMBER_NOTES)
  197.          ch->arp[2] = pitch_table[note][ch->finetune];
  198.       else
  199.          {
  200.          status("Arpeggio note out of range");
  201.          error = FAULT;
  202.          }
  203.       ch->arpindex = 0;
  204.       ch->adjust = do_arpeggio;
  205.       }
  206.    }
  207.  
  208. /* volume slide. Mostly used to simulate waveform control.
  209.  * (attack/decay/sustain).
  210.  */
  211. LOCAL void do_slidevol(ch)
  212. struct channel *ch;
  213.    {
  214.    set_current_volume(ch, ch->volume + ch->volumerate);
  215.    }
  216.  
  217. /* note that volumeslide does not have a ``take default''
  218.  * behavior. If para is 0, this is truly a 0 volumeslide.
  219.  * Issue: is the test really necessary ? Can't we do
  220.  * a HI(para) - LOW(para). Answer: protracker does not.
  221.  */
  222. LOCAL void parse_slidevol(ch, para)
  223. struct channel *ch;
  224. int para;
  225.    {
  226.    if (LOW(para))
  227.       ch->volumerate = -LOW(para);
  228.    else
  229.       ch->volumerate = HI(para);
  230.    }
  231.  
  232. LOCAL void set_slidevol(a, ch)
  233. struct automaton *a;
  234. struct channel *ch;
  235.    {
  236.    ch->adjust = do_slidevol;
  237.    parse_slidevol(ch, a->para);
  238.    }
  239.  
  240. /* portamento: gets from a given pitch to another.
  241.  * We can simplify the routine by cutting it in
  242.  * a pitch up and pitch down part while setting up
  243.  * the effect.
  244.  */
  245. LOCAL void do_portamento(ch)
  246. struct channel *ch;
  247.    {
  248.    if (ch->pitch < ch->pitchgoal)
  249.       {
  250.       ch->pitch += ch->pitchrate;
  251.       ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  252.       }
  253.    else if (ch->pitch > ch->pitchgoal)
  254.       {
  255.       ch->pitch -= ch->pitchrate;
  256.       ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  257.       }
  258.       /* if we want to implement funk glissando, we need a change right
  259.        * there
  260.        */
  261.    set_current_pitch(ch, ch->pitch);
  262.    }
  263.  
  264. /* if para and pitch are 0, this is obviously a continuation
  265.  * of the previous portamento.
  266.  */
  267. LOCAL void set_portamento(a, ch)
  268. struct automaton *a;
  269. struct channel *ch;
  270.    {
  271.    ch->adjust = do_portamento;
  272.    if (a->para)
  273.       ch->pitchrate = a->para;
  274.    if (a->pitch)
  275.       ch->pitchgoal = a->pitch;
  276.    }
  277.  
  278. /*
  279.  * combined commands.
  280.  */
  281. LOCAL void do_portaslide(ch)
  282. struct channel *ch;
  283.    {
  284.    do_portamento(ch);
  285.    do_slidevol(ch);
  286.    }
  287.  
  288. LOCAL void set_portaslide(a, ch)
  289. struct automaton *a;
  290. struct channel *ch;
  291.    {
  292.    ch->adjust = do_portaslide;
  293.    if (a->pitch)
  294.       ch->pitchgoal = a->pitch;
  295.    parse_slidevol(ch, a->para);
  296.    }
  297.  
  298. LOCAL void do_vibratoslide(ch)
  299. struct channel *ch;
  300.    {
  301.    do_vibrato(ch);
  302.    do_slidevol(ch);
  303.    }
  304.  
  305. LOCAL void set_vibratoslide(a, ch)
  306. struct automaton *a;
  307. struct channel *ch;
  308.    {
  309.    ch->adjust = do_vibratoslide;
  310.    parse_slidevol(ch, a->para);
  311.    }
  312.  
  313. /***
  314.  *
  315.  *  effects that just need a setup part
  316.  *
  317.  ***/
  318.  
  319. /* IMPORTANT: because of the special nature of
  320.  * the player, we can't process each effect independently,
  321.  * we have to merge effects from the four channel before
  322.  * doing anything about it. For instance, there can be 
  323.  * several speed change in the same note.
  324.  */
  325. LOCAL void set_speed(a, ch)
  326. struct automaton *a;
  327. struct channel *ch;
  328.    {
  329.    if (a->para >= 32 && get_pref_scalar(PREF_SPEEDMODE) != OLD_SPEEDMODE)
  330.       {
  331.       a->new_finespeed = a->para;
  332.       a->do_stuff |= SET_FINESPEED;
  333.       }
  334.    else if (a->para)
  335.       {
  336.       a->new_speed = a->para;
  337.       a->do_stuff |= SET_SPEED;
  338.       }
  339.    }
  340.  
  341. LOCAL void set_skip(a, ch)
  342. struct automaton *a;
  343. struct channel *ch;
  344.    {
  345.       /* BCD decoding in read.c */
  346.    a->new_note = a->para;
  347.    a->do_stuff |= SET_SKIP;
  348.    }
  349.  
  350. LOCAL void set_fastskip(a, ch)
  351. struct automaton *a;
  352. struct channel *ch;
  353.    {
  354.    a->new_pattern = a->para;
  355.    a->do_stuff |= SET_FASTSKIP;
  356.    }
  357.  
  358. /* immediate effect: starts the sample somewhere
  359.  * off the start.
  360.  */
  361. LOCAL void set_offset(a, ch)
  362. struct automaton *a;
  363. struct channel *ch;
  364.    {
  365.    set_position(ch, a->para * 256);
  366.    }
  367.  
  368. /* change the volume of the current channel.
  369.  * Is effective until there is a new set_volume,
  370.  * slide_volume, or an instrument is reloaded 
  371.  * explicitly by giving its number. Obviously, if
  372.  * you load an instrument and do a set_volume in the
  373.  * same note, the set_volume will take precedence.
  374.  */
  375. LOCAL void set_volume(a, ch)
  376. struct automaton *a;
  377. struct channel *ch;
  378.    {
  379.    set_current_volume(ch, a->para);
  380.    }
  381.  
  382.  
  383.  
  384. /***
  385.  *
  386.  * EXTENDED COMMANDS
  387.  *
  388.  ***/
  389.  
  390. /* extended command: retrig note at a fast pace
  391.  */
  392. LOCAL void do_retrig(ch)
  393. struct channel *ch;
  394.    {
  395.    if (--ch->current <= 0)
  396.       {
  397.       reset_note(ch, ch->note, ch->pitch);
  398.       ch->current = ch->retrig;
  399.       }
  400.    }
  401.  
  402. LOCAL void set_retrig(a, ch)
  403. struct automaton *a;
  404. struct channel *ch;
  405.    {
  406.    ch->retrig = a->para;
  407.    ch->current = ch->retrig;
  408.    ch->adjust = do_retrig;
  409.    }
  410.  
  411. /* extended command: start note after a small
  412.  * delay
  413.  */
  414. LOCAL void do_latestart(ch)
  415. struct channel *ch;
  416.    {
  417.    if (--ch->current <= 0)
  418.       {
  419.       reset_note(ch, ch->note, ch->pitch);
  420.       ch->adjust = do_nothing;
  421.       }
  422.    }
  423.  
  424. LOCAL void set_late_start(a, ch)
  425. struct automaton *a;
  426. struct channel *ch;
  427.    {
  428.    play_note(ch->audio, empty_sample() , 0);
  429.    ch->current = a->para;
  430.    ch->adjust = do_latestart;
  431.    }
  432.  
  433. /* extended command: cut note after some time.
  434.  * Note we only kill the volume, as protracker does...
  435.  */
  436. LOCAL void do_cut(ch)
  437. struct channel *ch;
  438.    {
  439.    if (ch->retrig)
  440.       {
  441.       if (--ch->retrig == 0)
  442.          set_current_volume(ch, 0);
  443.       }
  444.    }
  445.  
  446. LOCAL void set_note_cut(a, ch)
  447. struct automaton *a;
  448. struct channel *ch;
  449.    {
  450.    ch->retrig = a->para;
  451.    ch->adjust = do_cut;
  452.    }
  453.  
  454.  
  455. LOCAL void set_smooth_up(a, ch)
  456. struct automaton *a;
  457. struct channel *ch;
  458.    {
  459.    ch->pitch += a->para;
  460.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  461.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  462.    set_current_pitch(ch, ch->pitch);
  463.    }
  464.  
  465. LOCAL void set_smooth_down(a, ch)
  466. struct automaton *a;
  467. struct channel *ch;
  468.    {
  469.    ch->pitch -= a->para;
  470.    ch->pitch = MIN(ch->pitch, MAX_PITCH);
  471.    ch->pitch = MAX(ch->pitch, MIN_PITCH);
  472.    set_current_pitch(ch, ch->pitch);
  473.    }
  474.  
  475. LOCAL void set_change_finetune(a, ch)
  476. struct automaton *a;
  477. struct channel *ch;
  478.    {
  479.    ch->finetune = a->para;
  480.    }
  481.  
  482.  
  483. LOCAL void set_loop(a, ch)
  484. struct automaton *a;
  485. struct channel *ch;
  486.    {
  487.       /* Note: the current implementation of protracker
  488.        * does not allow for a jump from pattern to pattern,
  489.        * even though it looks like a logical extension to the current 
  490.        * format.
  491.        */
  492.    if (a->para == 0) 
  493.       a->loop_note_num = a->note_num;
  494.    else
  495.       {
  496.       if (a->loop_counter == 0)
  497.          a->loop_counter = a->para + 1;
  498.       /* We have to defer the actual count-down and note jump
  499.        * to automaton.c, because some modules include several
  500.        * loops on the same measure, which is a bit confusing
  501.        * (see don't you want me)
  502.        */
  503.       a->do_stuff |= JUMP_PATTERN;
  504.       }
  505.    }
  506.  
  507. LOCAL void set_smooth_upvolume(a, ch)
  508. struct automaton *a;
  509. struct channel *ch;
  510.    {
  511.    set_current_volume(ch, ch->volume + a->para);
  512.    }
  513.  
  514. LOCAL void set_smooth_downvolume(a, ch)
  515. struct automaton *a;
  516. struct channel *ch;
  517.    {
  518.    set_current_volume(ch, ch->volume - a->para);
  519.    }
  520.  
  521.  
  522. LOCAL void set_delay_pattern(a, ch)
  523. struct automaton *a;
  524. struct channel *ch;
  525.    {
  526.    a->counter -= (a->para + 1) * a->speed;
  527.    a->do_stuff |= DELAY_PATTERN;
  528.    }
  529.  
  530.  
  531.  
  532. /* Initialize the whole effect table */
  533.  
  534. void init_effects(table)
  535. void (*table[]) P((struct automaton *a, struct channel *ch));
  536.    {
  537.    int i;
  538.  
  539.    for (i = 0; i < NUMBER_EFFECTS; i++)
  540.       table[i] = set_nothing;
  541.    table[EFF_ARPEGGIO] = set_arpeggio;
  542.    table[EFF_SPEED] = set_speed;
  543.    table[EFF_SKIP] = set_skip;
  544.    table[EFF_FF] = set_fastskip;
  545.    table[EFF_VOLUME] = set_volume;
  546.    table[EFF_VOLSLIDE] = set_slidevol;
  547.    table[EFF_OFFSET] = set_offset;
  548.    table[EFF_PORTA] = set_portamento;
  549.    table[EFF_PORTASLIDE] = set_portaslide;
  550.    table[EFF_UP] = set_upslide;
  551.    table[EFF_DOWN] = set_downslide;
  552.    table[EFF_VIBRATO] = set_vibrato;
  553.    table[EFF_VIBSLIDE] = set_vibratoslide;
  554.    table[EFF_SMOOTH_UP] = set_smooth_up;
  555.    table[EFF_SMOOTH_DOWN] = set_smooth_down;
  556.    table[EFF_CHG_FTUNE] = set_change_finetune;
  557.    table[EFF_LOOP] = set_loop;
  558.    table[EFF_RETRIG] = set_retrig;
  559.    table[EFF_S_UPVOL] = set_smooth_upvolume;
  560.    table[EFF_S_DOWNVOL] = set_smooth_downvolume;
  561.    table[EFF_NOTECUT] = set_note_cut;
  562.    table[EFF_LATESTART] = set_late_start;
  563.    table[EFF_DELAY] = set_delay_pattern;
  564.    }
  565.  
  566.